home *** CD-ROM | disk | FTP | other *** search
- Path: solon.com!not-for-mail
- From: fred@genesis.demon.co.uk (Lawrence Kirby)
- Newsgroups: comp.lang.c.moderated,comp.std.c
- Subject: Behaviour of mem functions when n=0 ?
- Date: 3 Mar 1996 22:14:29 -0600
- Organization: none
- Sender: clc@solutions.solon.com
- Approved: clc@solutions.solon.com
- Message-ID: <4hdqn5$8qn@solutions.solon.com>
- Reply-To: fred@genesis.demon.co.uk
- NNTP-Posting-Host: solutions.solon.com
- X-Mailer: Demon Internet Simple News v1.27
-
- In article <4h75af$4ln@solutions.solon.com>
- chrisw@leland.stanford.edu "Chris Wilkins" writes:
-
- >
- > I was wondering if someone could tell me exactly what behaviour is guaranteed
- > for the mem library functions when n=0. I presume the answers are:
- >
- > memcpy() -> do nothing
- > memmove() -> do nothing
-
- The wording in the standard for both of these is 'copies n characters from the
- object'. There is nothing to make n==0 illegal so, yes.
-
- > memset() -> do nothing
-
- The standard wording is similar wording so this is correct.
-
- > memcmp() -> return 0
-
- Probably. However the result is defined by some rather unfortunate wording in
- the standard:
-
- "The memcmp function returns an integer greater than, equal to, or less than
- zero, accordingly as the object pointed to by s1 is greater than, equal to,
- or less than the object pointed to by s2"
-
- The problem here is that in the standard the term 'object' means
- something with a well-defined non-zero size. An object also has a type.
- It doesn't make much sense to describe the action of memcmp as comparing
- two objects since that doesn't provide any meaning to the comparison result.
- IMHO this is a defect in the standard. I've cross-posted to comp.std.c.
-
- > memchr() -> return NULL
-
- Again, probably. However there is a similar problem here to that with
- memcmp.
-
- "The memchr function returns a pointer to the located character, or a null
- pointer if the character does not occur in the object"
-
- If this had said "... does not occur in the initial n characters of the
- object" then fine. However it doesn't say that and you're back to trying
- to force the term 'object' to mean something it really can't.
-
- > But are these guaranteed?
- >
- > Also, when n=0 is it guaranteed that these functions will not dereference
- > their other arguments?
-
- There is no explicit guarantee.
-
- > Basically, I'm interested whether I can safely use something like:
- >
- > memcpy(ptr,NULL,0);
-
- No. It is explicit that the pointer arguments to these functions have to
- point to objects. The functions could legally contain code to the effect of:
-
- if (arg == NULL) abort();
-
- > or whether I have to wrap it:
- >
- > if(len>0) {
- > memcpy(dest,src,len);
- > }
-
- Yes, assuming that len>0 guarantees that dest and src are legal pointers
- to objects of suitable size (e.g. not null pointers).
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-